package cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.controller.admin.supplier.vo;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//腾讯地图api
@Data
public class TencentMapDemo {

    String apiUrl = "https://apis.map.qq.com/ws/location/v1/ip";
    static String apiKey = "QVOBZ-ER46R-XEHWZ-WOVHO-42LX2-XBFLK"; // 替换为你的腾讯地图API密钥

    public Map<String,Double> getAddress(){
        Map<String,Double> map=new HashMap<>();
        try {
            // 构建请求URL
            String requestUrl = apiUrl + "?key=" + apiKey;

            // 发送HTTP GET请求
            URL url = new URL(requestUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 获取响应结果
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 解析JSON响应
                JSONObject json =JSONObject.fromObject(JSONObject.fromObject(JSONObject.fromObject(response.toString()).getString("result")).getString("location"));
                Double lat = json.getDouble("lat");
                Double lng = json.getDouble("lng");
                map.put("lat",lat);
                map.put("lng",lng);
                System.out.println(lat+","+lng);
                // 在这里处理JSON响应并提取当前坐标信息
            } else {
                System.out.println("HTTP GET request failed with response code: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public static String getDistance(double lat1,double lng1,double lat2,double lng2){
        String result="";
        try {
            String apiUrl = "https://apis.map.qq.com/ws/distance/v1/";
//            // 坐标1
//            double lat1 = 39.90923; // 纬度
//            double lng1 = 116.397428; // 经度
//            // 坐标2
//            double lat2 = 40.088963; // 纬度
//            double lng2 = 116.413554; // 经度

            // 构建请求URL
            String requestUrl = apiUrl + "?mode=driving" +
                    "&from=" + URLEncoder.encode(lat1 + "," + lng1, "UTF-8") +
                    "&to=" + URLEncoder.encode(lat2 + "," + lng2, "UTF-8") +
                    "&key=" + apiKey;

            // 发送HTTP GET请求
            URL url = new URL(requestUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 获取响应结果
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 解析JSON响应
                String jsonResponse = response.toString();
                List<Object> objects = JSON.parseArray(JSONObject.fromObject(JSONObject.fromObject(jsonResponse).getString("result")).getString("elements"), Object.class);
                result = JSONObject.fromObject(objects.get(0)).getString("distance");
                System.out.println(result); // 打印JSON响应

                // 在这里处理JSON响应并提取距离信息
            } else {
                System.out.println("HTTP GET request failed with response code: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {

    }

}
